// pages/friends/createAccount/index.ts
import tran from '../../../utils/language';
import { eqTestQuestion, eqTestOver } from '../../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: tran._t('eqTestQuestion'),
    region: {},
    isEdit: getApp().globalData.profileFrom === 'edit',
    filterData: [{ text: '非常不同意', key: '321230', selectFlag: true }, { text: '不同意', key: '321431', selectFlag: false }, { text: '中性的', key: '321000', selectFlag: false }, { text: '同意', key: '321002', selectFlag: false }, { text: '非常同意', key: '322000', selectFlag: false }],
    questionTotalNum: 1,
    questionCurrentNum: 0,
    sliderNum: 0,
    questionList: [],
    currentQuestion: {
      questionId: ''
    },
    info: {},
    currentIndex: 0, // 选择选项的下标
    isTest: false, // 是否展示上方的重新情商测试的内容
    selectIndexList: [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options: any) {
    if (Object.keys(options).length > 0) {
      this.setData({
        info: JSON.parse(options.info),
        isTest: true
      })
    }
    this.getQuestion()
  },
  getQuestion () {
    const params = {}
    eqTestQuestion(params).then((res: any) => {
      console.log(res);
      this.setData({
        questionList: res,
        questionTotalNum: res.length || 0,
        currentQuestion: res && res.length > 0 ? res[0] : {}
      }, () => {
        // 这里写this.data.是为了拿到最新的值
        this.changeSilderNum(this.data.questionCurrentNum, this.data.questionTotalNum)
      })
    })
  },
  // 返回
  goBack () {
    // wx.navigateBack()
    wx.switchTab({
      url: '/pages/metrics/index'
    })
    // wx.navigateBack({
    //   delta: 1
    // })
  },
  /**
   * 计算滑块百分比
   * @param total 
   * @param num 
   */
  changeSilderNum (num: any, total: any) {
    let percentageNum: any = (num / total) * 100
    this.setData({
      sliderNum: percentageNum
    })
  },
  /**
   * 处理回显答案
   */
  handleSelect() {
    let { questionCurrentNum, questionList, selectIndexList, filterData } = this.data
    let currentIndex = selectIndexList[questionCurrentNum] || 0
    filterData.length > 0 && filterData.map((item, index) => {
      if (index === currentIndex) {
        item.selectFlag = true
      } else {
        item.selectFlag = false
      }
    })
    this.setData({
      filterData,
      currentIndex
    }, () => {
      this.setScore(currentIndex)
    })
  },
  /**
   * 上一题
   */
  previousTap () {
    let { questionCurrentNum, questionList } = this.data
    questionCurrentNum--;
    // 调用计算滑块的值的方法
    this.changeSilderNum(questionCurrentNum, this.data.questionTotalNum)
    this.setData({
      questionCurrentNum: questionCurrentNum,
      currentQuestion: questionCurrentNum > 0 ? questionList[questionCurrentNum - 1] :questionList[0]
    }, () => {
      this.handleSelect()
    })
  },
  /**
   * 重置选项
   */
  resetFilterData () {
    const { filterData } = this.data
    filterData.map((item: any, index: any) => {
      if (index == 0) {
        item.selectFlag = true
      } else {
        item.selectFlag = false
      }
    })
    this.setData({
      filterData,
    }, () => {
      // this.setScore(this.data.currentIndex || 0)
    })
  },
  /**
   * 下一题
   */
  nextTap () {
    let { questionCurrentNum, questionList, currentIndex, selectIndexList } = this.data
    questionCurrentNum++;
    // this.setScore(currentIndex || 0)
    selectIndexList[questionCurrentNum - 1] = currentIndex
    // this.toSelectPage('')
    // 调用计算滑块的值的方法
    this.changeSilderNum(questionCurrentNum, this.data.questionTotalNum)
    this.setData({
      questionCurrentNum: questionCurrentNum,
      // currentQuestion: questionList[questionCurrentNum * 1 + 1]
      currentQuestion: questionList[questionCurrentNum]
    }, () => {
      this.handleSelect()
      // this.resetFilterData()
    })
  },
  /**
   * 点击完成
   */
  testOver () {
    const { questionList } = this.data
    const list:any = []
    questionList.map((item: any) => {
      const obj = {
        questionId: item.questionId,
        score: item.score || 1
      }
      list.push(obj)
    })
    const params = list
    eqTestOver(params).then(res => {
      wx.showToast({
        title: '提交成功'
      })
      const resStr = JSON.stringify(res)
      setTimeout(() => {
        wx.reLaunch({
          url: `/pages/profile/eqTestResult/index?info=${resStr}`
        })
      }, 2000)
    })
  },
  toSelectPage(e: any) {
    let currentIndex = 0
    if (e) {
      currentIndex = e.target.dataset.index
    }
    const { filterData, questionList, currentQuestion } = this.data
    filterData.length > 0 && filterData.map((item, index) => {
      if (index === currentIndex) {
        item.selectFlag = true
      } else {
        item.selectFlag = false
      }
    })
    this.setData({
      filterData,
      currentIndex
    }, () => {
      this.setScore(currentIndex)
    })
  },
  /**
   * 设置分数
   * currentIndex: 选择选项的下标
   */
  setScore (index: any) {
    const { questionList, currentQuestion, questionCurrentNum,selectIndexList } = this.data
    // 倒序的分数
    const sequenceOrderScore = [1,2,3,4,5]
    // 倒序的分数
    const reverseOrderScore = [5,4,3,2,1]
    questionList.map((item: any,index: any) => {
      if (item.answerScoreAsc) { // 正序
        item.score = sequenceOrderScore[selectIndexList[index] || 0]
      } else { // 倒序
        item.score = reverseOrderScore[selectIndexList[index] || 0]
      }
    })
    this.setData({
      questionList,
    }, () => {
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})